/*
 * @Author: dadadaXU 1413107032@qq.com
 * @Date: 2025-01-23 11:04:41
 * @LastEditors: dadadaXU 1413107032@qq.com
 * @LastEditTime: 2025-01-23 11:10:46
 * @FilePath: \LeetCode\11.盛最多水的容器_01.cpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/*
 * @lc app=leetcode.cn id=11 lang=cpp
 *
 * [11] 盛最多水的容器
 * 
 * 方法1：快排算法中的双指针 + 贪心
 * - 使用贪心反而增加了计算的复杂度 -> 问题考虑复杂化了
 * - + 数组必须遍历一遍，但是贪心带来的收益不如产生的开销
 * 
 * 官方题解：每次移动双指针中的较小者
 */

#include <iostream>
#include <vector>

using namespace std;

// @lc code=start
class Solution
{
public:
    int maxArea(vector<int> &height)
    {
        int head = 0, tail = height.size() - 1;
        int width = tail - head;
        int maxVolume = std::min(height[head], height[tail]) * width;
        while (width > 1)
        {
            if (height[head] >= height[tail])
            {
                do
                {
                    tail--;
                    width--;
                } while (width >= 1 && height[tail - 1] < height[head] && // 防止贪心过度
                         height[tail - 1] * (width - 1) >= height[tail] * width); // 贪心
            }
            else
            {
                do
                {
                    head++;
                    width--;
                } while (width >= 1 && height[tail] > height[head + 1] &&
                         height[head] * width <= height[head + 1] * (width - 1));
            }
            maxVolume = std::max(maxVolume, std::min(height[head], height[tail]) * width);
        }

        return maxVolume;
    }

    int maxArea_00(vector<int> &height)
    {
        int head = 0, tail = height.size() - 1;
        int width = tail - head;
        int maxVolume = 0;
        while (width >= 1)
        {
            maxVolume = std::max(maxVolume, std::min(height[head], height[tail]) * width);
            if (height[head] > height[tail])
                tail--;
            else
                head++;
            width--;
        }

        return maxVolume;
    }
};
// @lc code=end

int main(void)
{
    Solution solution;
    vector<int> height = {1, 1};

    cout << solution.maxArea(height) << endl;

    return 0;
}